package com.lengyu.iottransfer.gateway.tcp.sanyuanShuibiao;

import com.alibaba.fastjson.JSONObject;
import com.lengyu.iottransfer.gateway.common.ByteUtils;
import com.lengyu.iottransfer.gateway.config.MqttSendGateway;
import com.lengyu.iottransfer.gateway.dto.MqttDeviceCurrentDto;
import com.lengyu.iottransfer.gateway.service.IDeviceTransferService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

@Component
public class SanyuanTransfer {
    private static Logger logger = LoggerFactory.getLogger(SanyuanTransfer.class);

    //防止统计的时候不好分组，所以用了相同的deviceType
    private static String DEVICE_TYPE="ZHONGYI_NB_WATER_METER";
    private static String DEVICE_CODE_PREFIX = "sanyuan";

    @Resource
    private MqttSendGateway mqttSendGateway;

    @Autowired
    private IDeviceTransferService deviceTransferService;

    public void transforToMqttData(byte[] byteArray){
        logger.info("水表消息解析：" + ByteUtils.byteArrayToHexString(byteArray));
        //帧长度128
        if(byteArray.length == 128){
            if(ByteUtils.byteArrayToHexString(byteArray,0,1,true).equals("68")
            &&ByteUtils.byteArrayToHexString(byteArray,127,1,true).equals("16")){
                String type = ByteUtils.byteArrayToHexString(byteArray,2,1,false);
                String address=ByteUtils.byteArrayToHexString(byteArray,5,5,false);

                String deviceCode = new StringBuilder().append(type).append(address).toString();

                String flowData1 = ByteUtils.byteArrayToHexString(byteArray,15,3,false);
                String flowData2 = ByteUtils.byteArrayToHexString(byteArray,18,1,false);
                String flowDataStr = new StringBuilder().append(flowData1).append(".").append(flowData2).toString();
                BigDecimal flowData = new BigDecimal(flowDataStr);

                BigDecimal bv = BigDecimal.valueOf(ByteUtils.byteArrayToUnsignedInt(byteArray,93)).divide(new BigDecimal("100"),2, RoundingMode.HALF_UP);

                logger.info("设备编号：" + deviceCode + "电池电量：" + bv + "流量" + flowData);

                MqttDeviceCurrentDto mqttDeviceCurrentDto = new MqttDeviceCurrentDto();
                mqttDeviceCurrentDto.setTime(System.currentTimeMillis());
                mqttDeviceCurrentDto.setMethod("thing.event.property.post");

                JSONObject paramJson = new JSONObject();

                mqttDeviceCurrentDto.setDeviceCode(DEVICE_CODE_PREFIX + "_" + deviceCode);
                paramJson.put("cumulative_flow",flowData);
                paramJson.put("battery_status", bv);

                mqttDeviceCurrentDto.setParams(paramJson);

                logger.info("解析结果：" + JSONObject.toJSONString(mqttDeviceCurrentDto));

                //发送解析结果到物联网平台mqtt
                mqttSendGateway.sendToMqtt(deviceTransferService.getIotPlatPropertyTopicV1(DEVICE_TYPE), JSONObject.toJSONString(mqttDeviceCurrentDto));

                logger.info("发送mqtt完成：TOPIC:" + deviceTransferService.getIotPlatPropertyTopicV1(DEVICE_TYPE) +"  PAYLOAD:"+ JSONObject.toJSONString(mqttDeviceCurrentDto));
            }

            }

        }
}
